import { parse } from 'querystring';
import Long from 'long';
/* eslint no-useless-escape:0 import/prefer-default-export:0 */

const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
export const isUrl = path => reg.test(path);

// TODO: 需要检查配置,然后判断是否需要删除
export const isAntDesignPro = () => {
  if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
    return true;
  }

  return window.location.hostname === 'preview.pro.ant.design';
}; // 给官方演示站点用，用于关闭真实开发环境不需要使用的特性

// TODO: 需要检查是否需要删除
export const isAntDesignProOrDev = () => {
  const { NODE_ENV } = process.env;

  if (NODE_ENV === 'development') {
    return true;
  }

  return isAntDesignPro();
};
export const getPageQuery = () => parse(window.location.href.split('?')[1]);

// 将对象转换为字符串
export const objToString = (obj, prevKey = '') => {
  const prefix = prevKey ? `${prevKey}.` : '';
  return Object.entries(obj).reduce((prev, [key, value]) => {
    if (typeof value === 'object') {
      return {
        ...prev,
        ...objToString(value, prefix + key),
      };
    }
    prev[prefix + key] = value;
    return prev;
  }, {});
};

export function searchToMap(str) {
  const obj = {};
  let result = str;
  if (result.startsWith('?')) {
    result = result.substring(1);
  }
  const arr = result.split('&');
  arr.forEach(item => {
    if (typeof item === 'string' && item.length > 0) {
      const index = item.indexOf('=');
      if (index >= 0) {
        obj[item.substring(0, index)] = item.substring(index + 1);
      }
    }
  });
  return obj;
}

export function mapToSearch(url, map) {
  let result = '';

  Object.entries(map).forEach(([key, value]) => {
    const str = `${key}=${value}`;
    if (result.length > 0) {
      result += `&${str}`;
    } else {
      result = str;
    }
  });

  if (url.indexOf('?') >= 0) {
    result = `${url}&${result}`;
  } else {
    result = `${url}?${result}`;
  }
  return encodeURI(result);
}

/**
 * 将对象的 value 转换为成唯一 value.
 * 会修改原对象
 * @param object
 * @param prefix
 * @param func
 */
export function valueUniqueByKey(object, { prefix = '', func } = {}) {
  if (typeof object === 'object') {
    Object.entries(object).forEach(([key, value]) => {
      if (typeof value === 'object') {
        valueUniqueByKey(value, {
          prefix: prefix + key,
          func,
        });
      } else {
        object[key] = `${prefix}${key}${value}`;
      }
    });
  }
}

export function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}

export function stringToHex(str) {
  let value = str;
  if (value.length % 2 !== 0) {
    value = `0${value}`;
  }
  let s = '';
  while (value.length > 2) {
    s = value.substring(0, 2) + s;
    value = value.substring(2);
  }
  s = value + s;
  const n = Long.fromString(s, 16).toString(10);
  return n;
}
